import { useRequest } from "../../../utils/request";
import { flatten } from "../../../utils/utils";
import map from "../../../utils/map";
import { addHouseHistory } from "../../../utils/history";

Component({
    properties: {},
    data: {
        bannerCurrent: 1,
        statusText: {
            1: '空置中',
            2: '待上架',
            3: '审核中',
            4: '待释放',
            5: '已出租',
            6: '删除',
            7: '审核失败'
        },
        showEquipmentStatus: false,
    },
    observers: {
        data: function (data) {
            this.setData({
                addressText: data.province + data.city + data.area + data.address
            })
        }
    },
    methods: {
        onLoad(options) {
            this.setData({
                id: options.id,
                read: !!options.read
            })
            this.request = new useRequest('api/housedetail', {
                params: { house_id: options.id || 1, },
                pretreatment: data => {
                    // banner
                    data.banner = flatten(
                        Object.keys(data.house_imgs)
                            .map(key => data.house_imgs[key])
                            .sort((a, b) => a.index - b.index)
                            .map(item => item.imgs)
                    )

                    addHouseHistory({
                        house_id: options.id,
                        img: data.img,
                        title: data.title,
                        avatar: data.house_owner_avatar,
                        name: data.house_owner_name
                    })
                    return data
                },
                data: 'data',
                hold: true
            }, this)

            this.favoriteRequest = new useRequest('api/collectall', {
                params: { type: 1, house_id: options.id },
                loading: false,
                hold: true
            }, this)

            this.unFavoriteRequest = new useRequest('api/cancercollectdata', {
                params: { type: 1, house_id: options.id },
                loading: false,
                hold: true
            }, this)

            // 上架
            this.upHouseRequest = new useRequest('api/owneruphouse', { hold: true }, this)

            // 下架
            this.downHouseRequest = new useRequest('api/ownerdownhouse', { hold: true }, this)

            // 删除
            this.removeRequest = new useRequest('api/ownerdelhouse', { hold: true }, this)

            // 释放
            this.fanRequest = new useRequest('api/ownerreleasehouse', { hold: true }, this)
        },
        onShow() {
            this.request.run()
        },
        back() {
            if (getCurrentPages().length > 1) {
                wx.navigateBack()
            } else {
                wx.switchTab({
                    url: '/pages/home/index/index'
                })
            }
        },
        bannerChange(e) {
            this.setData({
                bannerCurrent: e.detail.current + 1
            })
        },
        previewBanner(e) {
            wx.previewImage({
                current: e.currentTarget.dataset.src,
                urls: [this.data.data.img, ...this.data.data.banner]
            })
        },
        // 底部
        createOrder() {
            wx.navigateTo({
                url: '/pages/order/create/create?id=' + this.data.id,
                success: result => {
                    result.eventChannel.emit('getHouseData', this.data.data)
                }
            })
        },
        callPhone() {
            wx.makePhoneCall({
                phoneNumber: this.data.data.house_owner_phone
            })
        },
        favorite() {
            if (this.data.data.is_collect) {
                this.unFavoriteRequest.run().then(() => {
                    this.setData({
                        ['data.is_collect']: 0
                    })
                })
            } else {
                this.favoriteRequest.run().then(() => {
                    this.setData({
                        ['data.is_collect']: 1
                    })
                })
            }
        },
        // 周边
        seeMap() {
            map.geocoder({
                address: this.data.addressText,
                success: res => {
                    const { lat, lng } = res.result.location
                    wx.openLocation({
                        latitude: lat,
                        longitude: lng,
                        name: this.data.data.title,
                        address: this.data.addressText
                    })
                }
            })
        },
        // 配套设施
        showEquipment() {
            this.setData({
                showEquipmentStatus: true
            })
        },
        hideEquipment() {
            this.setData({
                showEquipmentStatus: false
            })
        },
        // 上架
        up() {
            wx.showModal({
                title: '确认上架该房源',
                content: '上架后可对外出租',
                success: result => {
                    if (result.confirm) {
                        this.upHouseRequest.run({
                            house_id: this.data.id
                        }).then(() => {
                            this.setData({
                                ['data.status']: 1
                            })
                        })
                    }
                }
            })
        },
        // 下架
        down() {
            wx.showModal({
                title: '确定下架该房源',
                content: '下架之后不可再对外出租了',
                success: result => {
                    if (result.confirm) {
                        this.downHouseRequest.run({
                            house_id: this.data.id
                        }).then(() => {
                            this.setData({
                                ['data.status']: 2
                            })
                        })
                    }
                }
            })
        },
        // 释放
        fan() {
            wx.showModal({
                title: '确定释放该房源',
                content: '释放之后可对外出租',
                success: result => {
                    if (result.confirm) {
                        this.fanRequest.run({
                            house_id: this.data.id
                        }).then(() => {
                            this.setData({
                                ['data.status']: 1
                            })
                        })
                    }
                }
            })
        },
        // 编辑
        edit() {
            wx.navigateTo({
                url: '/pages/house/edit/index/index?id=' + this.data.id
            })
        },
        // 删除
        remove() {
            wx.showModal({
                title: '确认删除该房源',
                content: '确认后房源将永久删除',
                success: result => {
                    if (result.confirm) {
                        this.removeRequest.run({
                            house_id: this.data.id
                        }).then(() => {
                            wx.showToast({
                                title: '删除成功',
                                icon: 'success',
                                success() {
                                    wx.navigateBack()
                                }
                            })
                        })
                    }
                }
            })
        },
        // 分享
        onShareAppMessage() {
            return {
                title: '换去想去的地方旅游，赶紧预定吧',
                path: '/pages/house/detail/detail?id=' + this.data.id
            }
        }
    }
});
